import { REACT_ELEMENT, } from '../constant';
import Component from './component';
import wrap2VirtualDom from '../utils/wrap2VirtualDom';

function createElement(type, config, children) {
    // 定义 ref 和 key
    let ref = null, key = null;
    if(config) {
        ref = config?.ref;
        key = config?.key;
        delete config?.ref;
        delete config?.key;
        delete config?._source;
        delete config?._self;
    }
    // 定义 props
    const props = { ...config, };
    if(arguments.length > 3) { // 有多个子节点的情况
        props.children = Array.prototype.slice.call(arguments, 2).map(wrap2VirtualDom);
    }else { // 只有一个子节点的情况
        props.children = wrap2VirtualDom(children);
    }

    return {
        $$typeof: REACT_ELEMENT,
        type,
        ref,
        key,
        props,
    };
}

const React = {
    Component,
    createElement,
};

export default React;